/** 
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 ** 
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.	
 **	
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 ** 
 **/

package com.ushahidi.android.app.data;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

import com.ushahidi.android.app.ImageManager;
import com.ushahidi.android.app.UshahidiApplication;
import com.ushahidi.android.app.UshahidiService;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class HandleXml {

	public static List<IncidentsData> processIncidentsXml(String xml) {
		String categories = "";
		String thumbnail = "";
		String image = "";

		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<IncidentsData> listIncidentsData = new ArrayList<IncidentsData>();

		NodeList node = doc.getElementsByTagName("incident");

		for (int i = 0; i < node.getLength(); i++) {

			Node firstNode = node.item(i);
			IncidentsData incidentData = new IncidentsData();
			listIncidentsData.add(incidentData);

			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;

				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);
				NodeList id = idElement.getChildNodes();
				if(id.getLength()!=0)
					incidentData.setIncidentId(Integer.valueOf(id.item(0).getNodeValue()));

				incidentData.setIncidentId(Integer.parseInt((id.item(0))
						.getNodeValue()));

				NodeList titleElementList = element
						.getElementsByTagName("title");
				Element titleElement = (Element) titleElementList.item(0);
				NodeList title = titleElement.getChildNodes();
				if (title.getLength() != 0)
					incidentData.setIncidentTitle((title.item(0))
							.getNodeValue());

				NodeList descElementList = element
						.getElementsByTagName("description");
				Element descElement = (Element) descElementList.item(0);
				NodeList desc = descElement.getChildNodes();
				if (desc.getLength() != 0)
					incidentData.setIncidentDesc((desc.item(0)).getNodeValue());

				NodeList dateElementList = element.getElementsByTagName("date");
				Element dateElement = (Element) dateElementList.item(0);
				NodeList date = dateElement.getChildNodes();
				if (date.getLength() != 0)
					incidentData.setIncidentDate((date.item(0)).getNodeValue());

				NodeList modeElementList = element.getElementsByTagName("mode");
				Element modeElement = (Element) modeElementList.item(0);
				NodeList mode = modeElement.getChildNodes();
				if(mode.getLength()!=0)
					incidentData.setIncidentMode(Integer.parseInt((mode.item(0)).getNodeValue()));
				
				NodeList verifiedElementList = element
						.getElementsByTagName("verified");
				Element verifiedElement = (Element) verifiedElementList.item(0);
				NodeList verified = verifiedElement.getChildNodes();
				if(verified.getLength()!=0)
					incidentData.setIncidentVerified(Integer.parseInt((verified
						.item(0)).getNodeValue()));

				// location
				NodeList locationElementList = element
						.getElementsByTagName("location");
				Node locationNode = locationElementList.item(0);
				Element locationElement = (Element) locationNode;
				NodeList locationNameList = locationElement
						.getElementsByTagName("name");

				Element locationInnerNameElement = (Element) locationNameList
						.item(0);
				NodeList locationInnerName = locationInnerNameElement
						.getChildNodes();
				if (locationInnerName.getLength() != 0)
					incidentData
							.setIncidentLocation((locationInnerName.item(0))
									.getNodeValue());

				NodeList locationLatitudeList = locationElement
						.getElementsByTagName("latitude");
				Element locationInnerLatitudeElement = (Element) locationLatitudeList
						.item(0);
				NodeList locationInnerLatitude = locationInnerLatitudeElement
						.getChildNodes();
				if(locationInnerLatitude.getLength()!=0)
					incidentData.setIncidentLocLatitude((locationInnerLatitude
						.item(0)).getNodeValue());

				NodeList locationLongitudeList = locationElement
						.getElementsByTagName("longitude");

				Element locationInnerLongitudeElement = (Element) locationLongitudeList
						.item(0);
				NodeList locationInnerLongitude = locationInnerLongitudeElement
						.getChildNodes();
				if(locationInnerLongitude.getLength()!=0)
					incidentData.setIncidentLocLongitude((locationInnerLongitude
						.item(0)).getNodeValue());

				// categories
				NodeList categoryList = element
						.getElementsByTagName("category");
				for (int w = 0; w < categoryList.getLength(); w++) {
					Node categoryNode = categoryList.item(w);
					if (categoryNode.getNodeType() == Node.ELEMENT_NODE) {
						Element categoryElement = (Element) categoryNode;
						NodeList categoryNameList = categoryElement
								.getElementsByTagName("title");
						Element categoryInnerTitleElement = (Element) categoryNameList
								.item(0);
						NodeList categoryInnerTitle = categoryInnerTitleElement
								.getChildNodes();
						// TODO !!! Watch this!!
						// It simply links all category names with ',' and put
						// in categories field
						// eg. "XXX,XXXX,xXXX,xxXXx"
						categories += (w == categoryList.getLength() - 1) ? (categoryInnerTitle
								.item(0)).getNodeValue() : (categoryInnerTitle
								.item(0)).getNodeValue() + ",";
					}
				}

				incidentData.setIncidentCategories(categories);
				categories = "";
				// UshahidiService.mNewIncidentsImages.clear();
				// UshahidiService.mNewIncidentsThumbnails.clear();
				// categories
				NodeList mediaList = element.getElementsByTagName("media");
				for (int j = 0; j < mediaList.getLength(); j++) {

					Node mediaNode = mediaList.item(j);
					if (mediaNode.getNodeType() == Node.ELEMENT_NODE) {
						Element mediaElement = (Element) mediaNode;
						NodeList mediaThumbList = mediaElement
								.getElementsByTagName("thumb");

						if (mediaThumbList.getLength() != 0) {

							Element mediaInnerThumbElement = (Element) mediaThumbList
									.item(0);
							NodeList mediaThumb = mediaInnerThumbElement
									.getChildNodes();

							if (!(mediaThumb.item(0)).getNodeValue().equals("")) {

								UshahidiService.mNewIncidentsThumbnails
										.add((mediaThumb.item(0))
												.getNodeValue());
							}

							File thumbnailFilename = new File(
									(mediaThumb.item(0)).getNodeValue());
							thumbnail += (j == mediaList.getLength() - 1) ? thumbnailFilename
									.getName() : thumbnailFilename.getName()
									+ ",";
						}

						NodeList mediaImageList = mediaElement
								.getElementsByTagName("link");

						if (mediaImageList.getLength() != 0) {
							Element mediaInnerImageElement = (Element) mediaImageList
									.item(0);
							NodeList mediaImage = mediaInnerImageElement
									.getChildNodes();
							if (!(mediaImage.item(0)).getNodeValue().equals("")) {
								UshahidiService.mNewIncidentsImages
										.add((mediaImage.item(0))
												.getNodeValue());
							}
							// if( j != 0) {
							File imageFilename = new File(
									(mediaImage.item(0)).getNodeValue());
							image += imageFilename.getName() + ",";
							// }
							// image += (j == mediaImageList.getLength() -1)? (
							// (Node)mediaImage.item(0)).getNodeValue(): (
							// (Node)mediaImage.item(0)).getNodeValue()+",";

						}
					}
				}

				incidentData.setIncidentThumbnail(thumbnail);
				incidentData.setIncidentImage(image);
				thumbnail = "";
				image = "";

			}

		}

		// save images
		ImageManager.saveThumbnail();
		ImageManager.saveImage();

		return listIncidentsData;

	}

	public static List<ResourceCategoryData> processResource_CategoryXml(
			String xml) {
		String categories = "";
		String thumbnail = "";
		String image = "";

		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<ResourceCategoryData> listResourceCategoryData = new ArrayList<ResourceCategoryData>();

		NodeList node = doc.getElementsByTagName("resource_category");

		for (int i = 0; i < node.getLength(); i++) {

			Node firstNode = node.item(i);
			ResourceCategoryData resourceCategoryData = new ResourceCategoryData();
			listResourceCategoryData.add(resourceCategoryData);

			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;

				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);

				NodeList id = idElement.getChildNodes();

				resourceCategoryData.setResourceCategoryId(Integer.parseInt((id
						.item(0)).getNodeValue()));

				NodeList resource_idElementList = element
						.getElementsByTagName("resource_id");
				Element resource_idElement = (Element) resource_idElementList
						.item(0);

				NodeList resource_id = resource_idElement.getChildNodes();
				resourceCategoryData.setResourceCategoryResourceId(Integer
						.parseInt(resource_id.item(0).getNodeValue()));// .setIncidentTitle((title.item(0)).getNodeValue());

				NodeList category_idElementList = element
						.getElementsByTagName("category_id");
				Element category_idElement = (Element) category_idElementList
						.item(0);

				NodeList category_id = category_idElement.getChildNodes();
				resourceCategoryData.setResourceCategoryCategoryId(Integer
						.parseInt(category_id.item(0).getNodeValue()));

			}

		}
		// save images

		return listResourceCategoryData;

	}

	// OceanMaster
	// TODO This a notice that
	// Matt exchanged fields of Categories from "XXX" to "category_XXX" except
	// for id
	// Talk with them to change it back to be compatible
	public static List<CategoriesData> processCategoriesXml(String xml) {
		// Log.v("Oceanmaster", "Categories XML to process is ["+xml+"]");
		List<CategoriesData> categoriesData = new ArrayList<CategoriesData>();
		String categories = "";
		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		NodeList node = doc.getElementsByTagName("category");
		for (int i = 0; i < node.getLength(); i++) {
			Node firstNode = node.item(i);
			CategoriesData category = new CategoriesData();
			categoriesData.add(category);

			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;

				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);

				NodeList id = idElement.getChildNodes();
				category.setCategoryId(Integer.parseInt((id.item(0))
						.getNodeValue()));

				NodeList titleElementList = element
						.getElementsByTagName("category_title");
				if (titleElementList.item(0) == null)
					titleElementList = element.getElementsByTagName("title");

				Element titleElement = (Element) titleElementList.item(0);

				NodeList title = titleElement.getChildNodes();
				category.setCategoryTitle((title.item(0)).getNodeValue());
				categories += (title.item(0)).getNodeValue() + ", ";

				NodeList descElementList = element
						.getElementsByTagName("category_description");
				if (descElementList.item(0) == null)
					descElementList = element
							.getElementsByTagName("description");

				Element descElement = (Element) descElementList.item(0);

				NodeList desc = descElement.getChildNodes();
				category.setCategoryDescription((desc.item(0)).getNodeValue());

				NodeList dateElementList = element
						.getElementsByTagName("category_color");
				if (dateElementList.item(0) == null)
					dateElementList = element.getElementsByTagName("color");

				Element dateElement = (Element) dateElementList.item(0);

				NodeList date = dateElement.getChildNodes();
				category.setCategoryColor((date.item(0)).getNodeValue());

			}
		}

		return categoriesData;
	}

	// by yujie chen
	public static List<ResourceData> processResourceXml(String xml) {
		String categories = "";
		String thumbnail = "";
		String image = "";

		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<ResourceData> listResourceData = new ArrayList<ResourceData>();

		NodeList node = doc.getElementsByTagName("resource");
		for (int i = 0; i < node.getLength(); i++) {

			Node firstNode = node.item(i);
			ResourceData resourceData = new ResourceData();
			listResourceData.add(resourceData);

			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;

				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);

				NodeList id = idElement.getChildNodes();
				if(id.getLength()!=0)
				resourceData.setResourceId(Integer.parseInt((id.item(0))
						.getNodeValue()));

				NodeList titleElementList = element
						.getElementsByTagName("title");
				Element titleElement = (Element) titleElementList.item(0);

				NodeList title = titleElement.getChildNodes();
				if(title.getLength()!=0)
				resourceData.setResourceTitle((title.item(0)).getNodeValue());

				NodeList descElementList = element
						.getElementsByTagName("description");
				Element descElement = (Element) descElementList.item(0);
				NodeList desc = descElement.getChildNodes();
				if(desc.getLength()!=0)
					resourceData.setResourceDesc((desc.item(0)).getNodeValue());

				NodeList dateElementList = element.getElementsByTagName("date");
				Element dateElement = (Element) dateElementList.item(0);

				NodeList date = dateElement.getChildNodes();
				if(date.getLength()!=0)
				resourceData.setResourceDate((date.item(0)).getNodeValue());

				NodeList modeElementList = element.getElementsByTagName("mode");
				Element modeElement = (Element) modeElementList.item(0);

				NodeList mode = modeElement.getChildNodes();
				if(mode.getLength()!=0)
				resourceData.setResourceMode(Integer.parseInt((mode.item(0))
						.getNodeValue()));

				NodeList verifiedElementList = element
						.getElementsByTagName("verified");
				Element verifiedElement = (Element) verifiedElementList.item(0);

				NodeList verified = verifiedElement.getChildNodes();
				if(verified.getLength()!=0)
				resourceData.setResourceVerified(Integer.parseInt((verified
						.item(0)).getNodeValue()));

				// location
				NodeList locationElementList = element
						.getElementsByTagName("location");

				Node locationNode = locationElementList.item(0);

				Element locationElement = (Element) locationNode;
				NodeList locationNameList = locationElement
						.getElementsByTagName("name");

				Element locationInnerNameElement = (Element) locationNameList
						.item(0);
				NodeList locationInnerName = locationInnerNameElement
						.getChildNodes();
				// added by yujie to avoid single </>tag
				if (locationInnerName.getLength() != 0)
					resourceData
							.setResourceLocation((locationInnerName.item(0))
									.getNodeValue());

				NodeList locationLatitudeList = locationElement
						.getElementsByTagName("latitude");

				Element locationInnerLatitudeElement = (Element) locationLatitudeList
						.item(0);
				NodeList locationInnerLatitude = locationInnerLatitudeElement
						.getChildNodes();
				if(locationInnerLatitude.getLength()!=0)
				resourceData.setResourceLocLatitude((locationInnerLatitude
						.item(0)).getNodeValue());

				NodeList locationLongitudeList = locationElement
						.getElementsByTagName("longitude");

				Element locationInnerLongitudeElement = (Element) locationLongitudeList
						.item(0);
				NodeList locationInnerLongitude = locationInnerLongitudeElement
						.getChildNodes();
				if(locationInnerLongitude.getLength()!=0)
				resourceData.setResourceLocLongitude((locationInnerLongitude
						.item(0)).getNodeValue());

				// categories
				NodeList categoryList = element
						.getElementsByTagName("category");
				for (int w = 0; w < categoryList.getLength(); w++) {
					Node categoryNode = categoryList.item(w);
					if (categoryNode.getNodeType() == Node.ELEMENT_NODE) {
						Element categoryElement = (Element) categoryNode;
						NodeList categoryNameList = categoryElement
								.getElementsByTagName("title");
						Element categoryInnerTitleElement = (Element) categoryNameList
								.item(0);
						NodeList categoryInnerTitle = categoryInnerTitleElement
								.getChildNodes();
						// TODO !!! Watch this!!
						// It simply links all category names with ',' and put
						// in categories field
						// eg. "XXX,XXXX,xXXX,xxXXx"
						categories += (w == categoryList.getLength() - 1) ? (categoryInnerTitle
								.item(0)).getNodeValue() : (categoryInnerTitle
								.item(0)).getNodeValue() + ",";
					}
				}

				resourceData.setResourceCategories(categories);
				categories = "";
				// UshahidiService.mNewIncidentsImages.clear();
				// UshahidiService.mNewIncidentsThumbnails.clear();
				// categories
				NodeList mediaList = element.getElementsByTagName("media");
				for (int j = 0; j < mediaList.getLength(); j++) {

					Node mediaNode = mediaList.item(j);
					if (mediaNode.getNodeType() == Node.ELEMENT_NODE) {
						Element mediaElement = (Element) mediaNode;
						NodeList mediaThumbList = mediaElement
								.getElementsByTagName("thumb");

						if (mediaThumbList.getLength() != 0) {

							Element mediaInnerThumbElement = (Element) mediaThumbList
									.item(0);
							NodeList mediaThumb = mediaInnerThumbElement
									.getChildNodes();

							if (!(mediaThumb.item(0)).getNodeValue().equals("")) {

								UshahidiService.mNewResourceThumbnails
										.add((mediaThumb.item(0))
												.getNodeValue());
							}

							File thumbnailFilename = new File(
									(mediaThumb.item(0)).getNodeValue());
							thumbnail += (j == mediaList.getLength() - 1) ? thumbnailFilename
									.getName() : thumbnailFilename.getName()
									+ ",";
						}

						NodeList mediaImageList = mediaElement
								.getElementsByTagName("link");

						if (mediaImageList.getLength() != 0) {
							Element mediaInnerImageElement = (Element) mediaImageList
									.item(0);
							NodeList mediaImage = mediaInnerImageElement
									.getChildNodes();
							if (!(mediaImage.item(0)).getNodeValue().equals("")) {
								UshahidiService.mNewResourceImages
										.add((mediaImage.item(0))
												.getNodeValue());
							}
							// if( j != 0) {
							File imageFilename = new File(
									(mediaImage.item(0)).getNodeValue());
							image += imageFilename.getName() + ",";
							// }
							// image += (j == mediaImageList.getLength() -1)? (
							// (Node)mediaImage.item(0)).getNodeValue(): (
							// (Node)mediaImage.item(0)).getNodeValue()+",";

						}
					}
				}

				resourceData.setResourceThumbnail(thumbnail);
				resourceData.setResourceImage(image);
				thumbnail = "";
				image = "";

			}

		}

		// save images
		ImageManager.saveThumbnail();
		ImageManager.saveImage();

		return listResourceData;

	}

	protected static ImageManager getImageManager() {
		return UshahidiApplication.mImageManager;
	}

	public static List<IncidentResourceLinkData> processIncidentResourceLinkXml(String xml) {
		String categories = "";
		String thumbnail = "";
		String image = "";
	
		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<IncidentResourceLinkData> IncidentResourceLinkData = new ArrayList<IncidentResourceLinkData>();
	/*
		NodeList node = doc.getElementsByTagName("incident");
	
		for (int i = 0; i < node.getLength(); i++) {
	
			Node firstNode = node.item(i);
			IncidentsData incidentData = new IncidentsData();
			listIncidentsData.add(incidentData);
	
			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;
	
				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);
				NodeList id = idElement.getChildNodes();
				if(id.getLength()!=0)
					incidentData.setIncidentId(Integer.valueOf(id.item(0).getNodeValue()));
	
				incidentData.setIncidentId(Integer.parseInt((id.item(0))
						.getNodeValue()));
	
				NodeList titleElementList = element
						.getElementsByTagName("title");
				Element titleElement = (Element) titleElementList.item(0);
				NodeList title = titleElement.getChildNodes();
				if (title.getLength() != 0)
					incidentData.setIncidentTitle((title.item(0))
							.getNodeValue());
	
				NodeList descElementList = element
						.getElementsByTagName("description");
				Element descElement = (Element) descElementList.item(0);
				NodeList desc = descElement.getChildNodes();
				if (desc.getLength() != 0)
					incidentData.setIncidentDesc((desc.item(0)).getNodeValue());
	
				NodeList dateElementList = element.getElementsByTagName("date");
				Element dateElement = (Element) dateElementList.item(0);
				NodeList date = dateElement.getChildNodes();
				if (date.getLength() != 0)
					incidentData.setIncidentDate((date.item(0)).getNodeValue());
	
				NodeList modeElementList = element.getElementsByTagName("mode");
				Element modeElement = (Element) modeElementList.item(0);
				NodeList mode = modeElement.getChildNodes();
				if(mode.getLength()!=0)
					incidentData.setIncidentMode(Integer.parseInt((mode.item(0)).getNodeValue()));
				
				NodeList verifiedElementList = element
						.getElementsByTagName("verified");
				Element verifiedElement = (Element) verifiedElementList.item(0);
				NodeList verified = verifiedElement.getChildNodes();
				if(verified.getLength()!=0)
					incidentData.setIncidentVerified(Integer.parseInt((verified
						.item(0)).getNodeValue()));
	
				// location
				NodeList locationElementList = element
						.getElementsByTagName("location");
				Node locationNode = locationElementList.item(0);
				Element locationElement = (Element) locationNode;
				NodeList locationNameList = locationElement
						.getElementsByTagName("name");
	
				Element locationInnerNameElement = (Element) locationNameList
						.item(0);
				NodeList locationInnerName = locationInnerNameElement
						.getChildNodes();
				if (locationInnerName.getLength() != 0)
					incidentData
							.setIncidentLocation((locationInnerName.item(0))
									.getNodeValue());
	
				NodeList locationLatitudeList = locationElement
						.getElementsByTagName("latitude");
				Element locationInnerLatitudeElement = (Element) locationLatitudeList
						.item(0);
				NodeList locationInnerLatitude = locationInnerLatitudeElement
						.getChildNodes();
				if(locationInnerLatitude.getLength()!=0)
					incidentData.setIncidentLocLatitude((locationInnerLatitude
						.item(0)).getNodeValue());
	
				NodeList locationLongitudeList = locationElement
						.getElementsByTagName("longitude");
	
				Element locationInnerLongitudeElement = (Element) locationLongitudeList
						.item(0);
				NodeList locationInnerLongitude = locationInnerLongitudeElement
						.getChildNodes();
				if(locationInnerLongitude.getLength()!=0)
					incidentData.setIncidentLocLongitude((locationInnerLongitude
						.item(0)).getNodeValue());
	
				// categories
				NodeList categoryList = element
						.getElementsByTagName("category");
				for (int w = 0; w < categoryList.getLength(); w++) {
					Node categoryNode = categoryList.item(w);
					if (categoryNode.getNodeType() == Node.ELEMENT_NODE) {
						Element categoryElement = (Element) categoryNode;
						NodeList categoryNameList = categoryElement
								.getElementsByTagName("title");
						Element categoryInnerTitleElement = (Element) categoryNameList
								.item(0);
						NodeList categoryInnerTitle = categoryInnerTitleElement
								.getChildNodes();
						// TODO !!! Watch this!!
						// It simply links all category names with ',' and put
						// in categories field
						// eg. "XXX,XXXX,xXXX,xxXXx"
						categories += (w == categoryList.getLength() - 1) ? (categoryInnerTitle
								.item(0)).getNodeValue() : (categoryInnerTitle
								.item(0)).getNodeValue() + ",";
					}
				}
	
				incidentData.setIncidentCategories(categories);
				categories = "";
				// UshahidiService.mNewIncidentsImages.clear();
				// UshahidiService.mNewIncidentsThumbnails.clear();
				// categories
				NodeList mediaList = element.getElementsByTagName("media");
				for (int j = 0; j < mediaList.getLength(); j++) {
	
					Node mediaNode = mediaList.item(j);
					if (mediaNode.getNodeType() == Node.ELEMENT_NODE) {
						Element mediaElement = (Element) mediaNode;
						NodeList mediaThumbList = mediaElement
								.getElementsByTagName("thumb");
	
						if (mediaThumbList.getLength() != 0) {
	
							Element mediaInnerThumbElement = (Element) mediaThumbList
									.item(0);
							NodeList mediaThumb = mediaInnerThumbElement
									.getChildNodes();
	
							if (!(mediaThumb.item(0)).getNodeValue().equals("")) {
	
								UshahidiService.mNewIncidentsThumbnails
										.add((mediaThumb.item(0))
												.getNodeValue());
							}
	
							File thumbnailFilename = new File(
									(mediaThumb.item(0)).getNodeValue());
							thumbnail += (j == mediaList.getLength() - 1) ? thumbnailFilename
									.getName() : thumbnailFilename.getName()
									+ ",";
						}
	
						NodeList mediaImageList = mediaElement
								.getElementsByTagName("link");
	
						if (mediaImageList.getLength() != 0) {
							Element mediaInnerImageElement = (Element) mediaImageList
									.item(0);
							NodeList mediaImage = mediaInnerImageElement
									.getChildNodes();
							if (!(mediaImage.item(0)).getNodeValue().equals("")) {
								UshahidiService.mNewIncidentsImages
										.add((mediaImage.item(0))
												.getNodeValue());
							}
							// if( j != 0) {
							File imageFilename = new File(
									(mediaImage.item(0)).getNodeValue());
							image += imageFilename.getName() + ",";
							// }
							// image += (j == mediaImageList.getLength() -1)? (
							// (Node)mediaImage.item(0)).getNodeValue(): (
							// (Node)mediaImage.item(0)).getNodeValue()+",";
	
						}
					}
				}
	
				incidentData.setIncidentThumbnail(thumbnail);
				incidentData.setIncidentImage(image);
				thumbnail = "";
				image = "";
	
			}
	
		}
	
		// save images
		ImageManager.saveThumbnail();
		ImageManager.saveImage();
	*/
		return IncidentResourceLinkData;
	
	}
	// by yujie chen
	public static List<IncidentsData> processResourceXml_ReturnIncident(String xml) {
		String categories = "";
		String thumbnail = "";
		String image = "";

		DocumentBuilder builder = null;
		Document doc = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			doc = builder.parse(new InputSource(new StringReader(xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<IncidentsData> listResourceData = new ArrayList<IncidentsData>();

		NodeList node = doc.getElementsByTagName("resource");
		for (int i = 0; i < node.getLength(); i++) {

			Node firstNode = node.item(i);
			IncidentsData resourceData = new IncidentsData();
			listResourceData.add(resourceData);

			if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) firstNode;

				NodeList idElementList = element.getElementsByTagName("id");
				Element idElement = (Element) idElementList.item(0);

				NodeList id = idElement.getChildNodes();
				if(id.getLength()!=0)
				resourceData.setIncidentId(Integer.parseInt((id.item(0))
						.getNodeValue()));

				NodeList titleElementList = element
						.getElementsByTagName("title");
				Element titleElement = (Element) titleElementList.item(0);

				NodeList title = titleElement.getChildNodes();
				if(title.getLength()!=0)
				resourceData.setIncidentTitle((title.item(0)).getNodeValue());

				NodeList descElementList = element
						.getElementsByTagName("description");
				Element descElement = (Element) descElementList.item(0);
				NodeList desc = descElement.getChildNodes();
				if(desc.getLength()!=0)
					resourceData.setIncidentDesc((desc.item(0)).getNodeValue());

				NodeList dateElementList = element.getElementsByTagName("date");
				Element dateElement = (Element) dateElementList.item(0);

				NodeList date = dateElement.getChildNodes();
				if(date.getLength()!=0)
				resourceData.setIncidentDate((date.item(0)).getNodeValue());

				NodeList modeElementList = element.getElementsByTagName("mode");
				Element modeElement = (Element) modeElementList.item(0);

				NodeList mode = modeElement.getChildNodes();
				if(mode.getLength()!=0)
				resourceData.setIncidentMode(Integer.parseInt((mode.item(0))
						.getNodeValue()));

				NodeList verifiedElementList = element
						.getElementsByTagName("verified");
				Element verifiedElement = (Element) verifiedElementList.item(0);

				NodeList verified = verifiedElement.getChildNodes();
				if(verified.getLength()!=0)
				resourceData.setIncidentVerified(Integer.parseInt((verified
						.item(0)).getNodeValue()));

				// location
				NodeList locationElementList = element
						.getElementsByTagName("location");

				Node locationNode = locationElementList.item(0);

				Element locationElement = (Element) locationNode;
				NodeList locationNameList = locationElement
						.getElementsByTagName("name");

				Element locationInnerNameElement = (Element) locationNameList
						.item(0);
				NodeList locationInnerName = locationInnerNameElement
						.getChildNodes();
				// added by yujie to avoid single </>tag
				if (locationInnerName.getLength() != 0)
					resourceData
							.setIncidentLocation((locationInnerName.item(0))
									.getNodeValue());

				NodeList locationLatitudeList = locationElement
						.getElementsByTagName("latitude");

				Element locationInnerLatitudeElement = (Element) locationLatitudeList
						.item(0);
				NodeList locationInnerLatitude = locationInnerLatitudeElement
						.getChildNodes();
				if(locationInnerLatitude.getLength()!=0)
				resourceData.setIncidentLocLatitude((locationInnerLatitude
						.item(0)).getNodeValue());

				NodeList locationLongitudeList = locationElement
						.getElementsByTagName("longitude");

				Element locationInnerLongitudeElement = (Element) locationLongitudeList
						.item(0);
				NodeList locationInnerLongitude = locationInnerLongitudeElement
						.getChildNodes();
				if(locationInnerLongitude.getLength()!=0)
				resourceData.setIncidentLocLongitude((locationInnerLongitude
						.item(0)).getNodeValue());

				// categories
				NodeList categoryList = element
						.getElementsByTagName("category");
				for (int w = 0; w < categoryList.getLength(); w++) {
					Node categoryNode = categoryList.item(w);
					if (categoryNode.getNodeType() == Node.ELEMENT_NODE) {
						Element categoryElement = (Element) categoryNode;
						NodeList categoryNameList = categoryElement
								.getElementsByTagName("title");
						Element categoryInnerTitleElement = (Element) categoryNameList
								.item(0);
						NodeList categoryInnerTitle = categoryInnerTitleElement
								.getChildNodes();
						// TODO !!! Watch this!!
						// It simply links all category names with ',' and put
						// in categories field
						// eg. "XXX,XXXX,xXXX,xxXXx"
						categories += (w == categoryList.getLength() - 1) ? (categoryInnerTitle
								.item(0)).getNodeValue() : (categoryInnerTitle
								.item(0)).getNodeValue() + ",";
					}
				}

				resourceData.setIncidentCategories(categories);
				categories = "";
				// UshahidiService.mNewIncidentsImages.clear();
				// UshahidiService.mNewIncidentsThumbnails.clear();
				// categories
				NodeList mediaList = element.getElementsByTagName("media");
				for (int j = 0; j < mediaList.getLength(); j++) {

					Node mediaNode = mediaList.item(j);
					if (mediaNode.getNodeType() == Node.ELEMENT_NODE) {
						Element mediaElement = (Element) mediaNode;
						NodeList mediaThumbList = mediaElement
								.getElementsByTagName("thumb");

						if (mediaThumbList.getLength() != 0) {

							Element mediaInnerThumbElement = (Element) mediaThumbList
									.item(0);
							NodeList mediaThumb = mediaInnerThumbElement
									.getChildNodes();

							if (!(mediaThumb.item(0)).getNodeValue().equals("")) {

								UshahidiService.mNewResourceThumbnails
										.add((mediaThumb.item(0))
												.getNodeValue());
							}

							File thumbnailFilename = new File(
									(mediaThumb.item(0)).getNodeValue());
							thumbnail += (j == mediaList.getLength() - 1) ? thumbnailFilename
									.getName() : thumbnailFilename.getName()
									+ ",";
						}

						NodeList mediaImageList = mediaElement
								.getElementsByTagName("link");

						if (mediaImageList.getLength() != 0) {
							Element mediaInnerImageElement = (Element) mediaImageList
									.item(0);
							NodeList mediaImage = mediaInnerImageElement
									.getChildNodes();
							if (!(mediaImage.item(0)).getNodeValue().equals("")) {
								UshahidiService.mNewResourceImages
										.add((mediaImage.item(0))
												.getNodeValue());
							}
							// if( j != 0) {
							File imageFilename = new File(
									(mediaImage.item(0)).getNodeValue());
							image += imageFilename.getName() + ",";
							// }
							// image += (j == mediaImageList.getLength() -1)? (
							// (Node)mediaImage.item(0)).getNodeValue(): (
							// (Node)mediaImage.item(0)).getNodeValue()+",";

						}
					}
				}

				resourceData.setIncidentThumbnail(thumbnail);
				resourceData.setIncidentImage(image);
				thumbnail = "";
				image = "";

			}

		}

		// save images
		ImageManager.saveThumbnail();
		ImageManager.saveImage();

		return listResourceData;

	}

}
